package com.dqp.cslggroup.MenuViewPage;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.BaseActivity;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.ToastUtil;
import com.dqp.cslggroup.Util.UiUtils;
import com.dqp.cslggroup.bean.note;
import com.dqp.cslggroup.greendao.noteDao;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class noteManagement extends BaseActivity {
    private ListView listView;
    private static String takePhotoPath;
    private ImageView takePhotoImg;
    private int REQUEST_CODE_CAMERA = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jw_tip);
        initCmp();
        initListView();

    }

    private void initCmp(){
        Toolbar toolbar = findViewById(R.id.include_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        TextView seting=findViewById(R.id.seting);
        seting.setText(getResources().getString(R.string.icon_delete));
        seting.setOnClickListener(v -> ToastUtil.showToast("长按清空"));
        seting.setVisibility(View.VISIBLE);
        seting.setOnLongClickListener(v -> {
            MyApplication.getDaoInstant().getNoteDao().deleteAll();
            initListView();
            return true;
        });
        listView=findViewById(R.id.tip_list);
    }

    @SuppressLint("SetTextI18n")
    private void  initListView(){
        List<note> noteLists = MyApplication.getDaoInstant().getNoteDao().loadAll();
        ClassNoteAdapter noteAdapter = new ClassNoteAdapter(noteLists, this);
        listView.setAdapter(noteAdapter);
        listView.setOnItemClickListener((parent, view1, position, id) -> {
            note note = (com.dqp.cslggroup.bean.note) noteAdapter.getItem(position);
            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
            @SuppressLint("InflateParams") View view = LayoutInflater.from(this).inflate(R.layout.notes_dialog, null);
            builder.setView(view);
            TextView name = view.findViewById(R.id.note_course_name);
            TextView data = view.findViewById(R.id.note_data);
            EditText noteEd = view.findViewById(R.id.mynote);
            Button save = view.findViewById(R.id.btn_save);
            Button cancel = view.findViewById(R.id.btn_cancel);
            ImageView img = view.findViewById(R.id.img);
            TextView take = view.findViewById(R.id.take_picture);
            takePhotoImg = img;
            name.setText(note.getName());
            cancel.setText("删除");
            data.setText(note.getData());
            noteEd.setText(note.getText()+"");
            Dialog dialog = builder.show();
            UiUtils.setDialogWindowAttr(dialog, this);
            Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(android.R.color.transparent);
            cancel.setOnClickListener(v -> ToastUtil.showToast("长按删除"));
            cancel.setOnLongClickListener(v -> {
                MyApplication.getDaoInstant().getNoteDao().delete(note);
                List<note> noteLists1 = MyApplication.getDaoInstant().getNoteDao().loadAll();
                ClassNoteAdapter noteAdapter1 = new ClassNoteAdapter(noteLists1, this);
                listView.setAdapter(noteAdapter1);
                dialog.dismiss();
                return true;
            });
            take.setOnClickListener(v1 -> {
                StrictMode.VmPolicy.Builder builder1 = new StrictMode.VmPolicy.Builder();
                StrictMode.setVmPolicy(builder1.build());
                builder1.detectFileUriExposure();
                takePhotoPath = Environment.getExternalStorageDirectory().getPath() + "/CSLG_Note" + Calendar.getInstance().getTimeInMillis() + ".jpg";
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                Uri photoUri = Uri.fromFile(new File(takePhotoPath)); // 传递路径
                intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);// 更改系统默认存储路径
                startActivityForResult(intent, REQUEST_CODE_CAMERA);
            });
            save.setOnClickListener(v -> {
                UiUtils.hideKeyboard(view);
                note newNote = new note();
                newNote.setData(data.getText().toString());
                newNote.setName(name.getText().toString());
                newNote.setText(noteEd.getText().toString()+"");
                newNote.setStart(note.getStart());
                newNote.setWeek(note.getWeek());
                newNote.setImg(takePhotoPath);
                MyApplication.getDaoInstant().getNoteDao().delete(note);
                MyApplication.getDaoInstant().getNoteDao().insertOrReplace(newNote);
                List<note> noteLists1 = MyApplication.getDaoInstant().getNoteDao().loadAll();
                ClassNoteAdapter noteAdapter1 = new ClassNoteAdapter(noteLists1, this);
                listView.setAdapter(noteAdapter1);
                dialog.dismiss();
            });
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) {
            Glide.with(this).load(takePhotoPath).into(takePhotoImg);
            takePhotoImg.setVisibility(View.VISIBLE);

        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}
